import { useRouter, useRoute } from 'vue-router'
import type { IRegisterUser } from '@/typings/RegisterUser'
import { ref, onMounted, computed } from 'vue'
import type { Ref } from 'vue'
import { objToQuery } from '@/utils/tools'
import { getRegisterUserApi, postRegisterUserApi } from "@/api/userApi"
//元祖  这玩意还要搞的顺序一致呢
type RegisterUserList = [
    Ref<number>,
    Ref<IRegisterUser[]>,
    Ref<number>,
    Ref<number>,
    (page: number, size: number,form:any) => Promise<void>,
]

const useRegisterUserList = (): RegisterUserList => {
    const router = useRouter()
    const route = useRoute()

    const data = ref<IRegisterUser[]>([])
    const total = ref(0)

    const currentPage1 = computed(() => {
        return route.query.currentPage ? +route.query.currentPage : 1
    })
    const pageSize1 = computed(() => {
        return route.query.pageSize ? +route.query.pageSize : 10
    })


    const loadData = async (currentPage: number, pageSize: number, formdata?: Record<string, string> | undefined) => {
        router.replace(`?currentPage=${currentPage}&pageSize=${pageSize}`)
        // console.log(formdata);
        let query = ''
        if (typeof formdata != 'undefined') {
            query = objToQuery(formdata)
        }
        // console.log(query);
        if (typeof formdata != 'undefined') {
            //发formdata数据给后端
            var ret = await postRegisterUserApi(currentPage1.value, pageSize1.value, formdata)
        } else {
            var ret = await getRegisterUserApi(currentPage1.value, pageSize1.value)
        }
        if (ret.code === 0) {
            data.value = ret.data.usersdata
            total.value = ret.data.total
        }
    }
    onMounted(() => {
        loadData(currentPage1.value, pageSize1.value)
    })
    return [currentPage1, data, total, pageSize1, loadData]
}
export default useRegisterUserList